package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author 张琪
 * @date 2020/12/1
 * @describe
 */
@RestController
public class FileController {
    /**
     * url: http://localhost:8091/file
     * 参数: 文件信息 fileImage
     * 返回值: String
     *
     * @param fileImage 文件流
     * @return 返回成功
     */

    @Autowired
    private FileService fileService;

    @PostMapping("/file")
    public String saveFile(MultipartFile fileImage) throws IOException {
//        System.out.println(fileImage.getClass());
//        1. 获取文件名
        String name = fileImage.getOriginalFilename();

//        2. 指定目录
        String fileDirPath = "F:/image/";

//        3. 创建目录
        File fileDir = new File(fileDirPath);
        if (!fileDir.exists()) {
            fileDir.mkdir();
        }
//        4. 生成文件全路径

        String filePath = fileDirPath + name;
        File file = new File(filePath);
//        5. 文件上传
        fileImage.transferTo(file);
        return "文件上传成功";
    }

    /**
     * url: http://localhost/pic/upload?dir=image
     */
    @PostMapping("/pic/upload")
    public ImageVO doUpload(String dir, MultipartFile uploadFile) throws IOException {
       /* System.out.println(dir);
        System.out.println(uploadFile.getOriginalFilename());
        String fileDirPath = dir + "/";
        File fileDir = new File(fileDirPath);
        if(!fileDir.exists()){
            fileDir.mkdir();
        }
        String filePath = fileDirPath + uploadFile.getOriginalFilename();
        File file = new File(filePath);
        uploadFile.transferTo(file);

        return ImageVO.fault();*/

//        return fileService.upload1(dir,uploadFile);
        return fileService.upload(uploadFile, dir);
    }
}
